/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * window.java
 *
 * Created on Apr 30, 2010, 11:25:29 PM
 */

package client;

import java.awt.event.KeyEvent;

/**
 *
 * @author buyandalai
 */
public class clientWindow extends javax.swing.JFrame {
    client cl;
    /** Creates new form window */
    public clientWindow() {
        initComponents();
        this.disconnected();
    }

    public String getIpAddress()
    {
        return server_ip.getText();
    }

    public int getPort()
    {
        return Integer.parseInt(server_port.getText());
    }

    public void connected()
    {
        connect.setText("Disconnect");
        server_ip.setEnabled(false);
        server_port.setEnabled(false);
        history.setEnabled(true);
        write.setEnabled(true);
        send.setEnabled(true);
    }

    public void disconnected()
    {
        connect.setText("Connect");
        server_ip.setEnabled(true);
        server_port.setEnabled(true);
        history.setEnabled(false);
        write.setEnabled(false);
        send.setEnabled(false);
    }

    private void sendMessage()
    {
        String msg = this.write.getText().trim();
        if(msg.length() > 0)
        {
            cl.sendMessage(msg);
            this.addHistory("Me >> "+msg);
            this.write.setText("");
        }

    }


    public void serverReply(String msg)
    {
        this.addHistory(this.getIpAddress()+" >> "+msg);
    }

    private void addHistory(String str)
    {
        history.setText(str+"\n"+history.getText());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        label_server_ip = new javax.swing.JLabel();
        server_ip = new javax.swing.JTextField();
        label_server_port = new javax.swing.JLabel();
        server_port = new javax.swing.JTextField();
        connect = new javax.swing.JButton();
        label_message = new javax.swing.JLabel();
        send = new javax.swing.JButton();
        message_history = new javax.swing.JScrollPane();
        history = new javax.swing.JTextArea();
        message_write = new javax.swing.JScrollPane();
        write = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        label_server_ip.setText("Server IP: ");

        server_ip.setText("127.0.0.1");

        label_server_port.setText("Port");

        server_port.setText("6666");

        connect.setText("Connect");
        connect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connectActionPerformed(evt);
            }
        });

        label_message.setText("Message:");

        send.setText("Send");
        send.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendActionPerformed(evt);
            }
        });

        history.setColumns(20);
        history.setEditable(false);
        history.setRows(5);
        message_history.setViewportView(history);

        write.setColumns(20);
        write.setRows(5);
        write.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                write_keypressed(evt);
            }
        });
        message_write.setViewportView(write);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(message_history, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
                    .addComponent(message_write, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
                    .addComponent(send)
                    .addComponent(label_message)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(label_server_ip)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(server_ip, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(label_server_port)
                        .addGap(18, 18, 18)
                        .addComponent(server_port, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(connect))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(server_ip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label_server_ip)
                    .addComponent(server_port, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label_server_port))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(connect)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(message_history, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(label_message)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(message_write, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(send)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    private void connectActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        if(connect.getText().equals("Connect"))
        {
            cl = new client(this);
            cl.start();
        }
        else
        {
            cl.stop();
            this.disconnected();
        }
    }

    private void sendActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        this.sendMessage();
    }

    private void write_keypressed(java.awt.event.KeyEvent evt) {
        // TODO add your handling code here:
        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
        {
            this.sendMessage();
        }
    }



    // Variables declaration - do not modify
    private javax.swing.JButton connect;
    private javax.swing.JTextArea history;
    private javax.swing.JLabel label_message;
    private javax.swing.JLabel label_server_ip;
    private javax.swing.JLabel label_server_port;
    private javax.swing.JScrollPane message_history;
    private javax.swing.JScrollPane message_write;
    private javax.swing.JButton send;
    private javax.swing.JTextField server_ip;
    private javax.swing.JTextField server_port;
    private javax.swing.JTextArea write;
    // End of variables declaration

}
